package com.stadium.handler;

import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.stadium.bean.Equipment;
import com.stadium.bean.Order;
import com.stadium.bean.User;
import com.stadium.servlet.EquipmentServlet;
import com.stadium.servlet.OrderServlet;
import com.stadium.servlet.UserServlet;
import com.stadium.tools.ResultMessage;

@Controller
@RequestMapping(value = "views/")
public class OrderHandler {

	@Autowired
	private UserServlet userServlet;
	@Autowired
	private OrderServlet orderServlet;
	@Autowired
	private EquipmentServlet equipmentServlet;

	// 保存订单信息
	@RequiresRoles(value = { "user" })
	@RequestMapping(value = "saveOrder")
	public ResponseEntity<ResultMessage> saveOrder(
			@RequestParam(name = "number") Integer number,
			@RequestParam(name = "borrowName") String borrowName,
			@RequestParam(name = "telephone") String telephone,
			@RequestParam("equipId") Integer equipId) {
		System.out.println("equipId:" + equipId);
		Equipment equipment2 = equipmentServlet.getById(equipId);
		ResultMessage result =new ResultMessage();
		
		if ((equipment2.getRemainNum() - number) > 0) {
			// 获取用户账号
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession();
			String account = (String) session.getAttribute("account");

			Equipment equipment = new Equipment(equipId, null,0, null);
			Order order = new Order(null, 0, number, account, borrowName,
					telephone, new Date(), 0, 0, equipment);

			orderServlet.save(order);
			
			equipment2.setRemainNum(equipment2.getRemainNum() - number);
			equipmentServlet.save(equipment2);
			result.setResultInfo("申请成功！等待管理员审核!");
		} else {
			result.setResultInfo("器材余量不足!");
		}

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 取消订单
	@RequiresRoles(value = { "user" })
	@RequestMapping(value = "deleteOrder")
	public ResponseEntity<ResultMessage> deleteOrder(
			@RequestParam(name = "id") Integer id) {
		// 把租借出去器材数量加入到器材中数量总数量
		Order order = orderServlet.getById(id);
		Integer number = order.getNumber();
		Integer equipId = order.getEquipment().getId();
		Equipment equipment = equipmentServlet.getById(equipId);
		equipment.setRemainNum((equipment.getRemainNum() + number));
		equipmentServlet.save(equipment);

		orderServlet.delete(id);

		ResultMessage result = new ResultMessage();
		result.setResultInfo("取消成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 审核订单
	@RequiresRoles(value = { "equipManager" })
	@RequestMapping(value = "checkOrder")
	public ResponseEntity<ResultMessage> checkOrder() {
		// 返回所有未审核订单信息
		List<Order> orders = orderServlet.getAll(0);

		ResultMessage result = new ResultMessage();
		result.getResultParm().put("orders", orders);
		System.out.println("----orders:" + orders);
		result.setResultInfo("查询成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 审核订单通过
	@RequiresRoles(value = { "equipManager" })
	@RequestMapping(value = "checkedOrder")
	public ResponseEntity<ResultMessage> checkedOrder(
			@RequestParam(name = "id") Integer id) {
		// 获取对应订单信息，并修改订单状态
		Order order = orderServlet.getById(id);
		order.setStatus(1);
		orderServlet.save(order);

		ResultMessage result = new ResultMessage();
		result.setResultInfo("处理成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	@RequestMapping(value = "isreturnEquip")
	public ResponseEntity<ResultMessage> isreturnEquip(
			@RequestParam(name = "account") String account) {

		List<Order> orders = orderServlet.getByAccount(account);

		ResultMessage result = new ResultMessage();
		result.getResultParm().put("orders", orders);
		result.setResultInfo("查询成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 普通用户申请归还器材
	@RequiresRoles(value = { "user" })
	@RequestMapping(value = "applyForReturn")
	public ResponseEntity<ResultMessage> applyForReturn(
			@RequestParam(name = "id") Integer id) {
		ResultMessage result = new ResultMessage();
		Order order = orderServlet.getById(id);
		order.setApplyReturn(1);
		orderServlet.save(order);
		result.setResultInfo("请求成功，等待器材管理员审核");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 获取用户相应信息
	@RequiresRoles(value = { "user" })
	@RequestMapping(value = "getOrder")
	public ResponseEntity<ResultMessage> getOrder() {
		// 获取session，并在session中获取account的值
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		String account = (String) session.getAttribute("account");

		User user = userServlet.getByAccount(account);
		List<Order> orders = orderServlet.getByAccount(account);

		ResultMessage result = new ResultMessage();
		result.getResultParm().put("user", user);
		result.getResultParm().put("orders", orders);
		System.out.println("orders:" +orders);
		result.setResultInfo("查询成功!");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 显示所有用户申请归还器材的信息
	@RequiresRoles(value = { "equipManager" })
	@RequestMapping(value = "reviewApplication")
	public ResponseEntity<ResultMessage> reviewApplication() {
		List<Order> orders = orderServlet.getAllApplyOrder();

		ResultMessage result = new ResultMessage();
		result.getResultParm().put("orders", orders);
		result.setResultInfo("处理成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 在页面点击要归还的器材，并附带订单id，以便修改器材归还状态
	@RequiresRoles(value = { "equipManager" })
	@RequestMapping(value = "returnEquip")
	public ResponseEntity<ResultMessage> returnEquip(
			@RequestParam(name = "id") Integer id) {

		Order order = orderServlet.getById(id);
		order.setIsReturn(1);
		orderServlet.save(order);

		// 归还成功时，器材增加借出去的数量
		Integer number = order.getNumber();
		Integer euqipId = order.getEquipment().getId();
		Equipment equipment = equipmentServlet.getById(euqipId);
		equipment.setRemainNum((equipment.getRemainNum() + number));
		equipmentServlet.save(equipment);

		ResultMessage result = new ResultMessage();
		result.setResultInfo("处理成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
}
